package hot_100;

public class 将有序数组转换为二叉搜索树 {
    public static void main(String[] args) {

    }

    int[] s;

    public TreeNode sortedArrayToBST(int[] nums) {
        s = nums;
        return Dfs(0, nums.length);
    }

    public TreeNode Dfs(int l, int r) {
        if (l > r)
            return null;
        int mid = (l + r) >> 1;
        TreeNode root = new TreeNode(s[mid]);
        root.left = Dfs(l, mid - 1);
        root.right = Dfs(mid + 1, r);
        return root;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
